﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace GreenArrow
{
    class PickerStatusGetRect : PickerStatus
    {
        public bool dragging = false;
        public int startX;
        public int startY;
        public override void Init()
        {
            form.ImageBox.Cursor = form.cursorCross;
            //form.ImageBox.Cursor = Cursors.Cross;
        }
        public override void MouseDown(MouseEventArgs e) {
            if (!dragging)
            {
                startX = e.X;
                startY = e.Y;
                dragging = true;
            }
        }
        public override void MouseUp(MouseEventArgs e) {
            if (dragging)
            {
                dragging = false;
                int endX = e.X;
                int endY = e.Y;
                if (endX == startX || endY == startY) {
                    return;
                }
                form.result.Rects.Add(new GRect(startX, startY, endX, endY));
                ChangeStatus(new PickerStatusGetPoint());
            }
        }
        public override void MouseMove(MouseEventArgs e)
        {
            Graphics g;
            Bitmap buffer = new Bitmap(form.ImageBox.Image.Width, form.ImageBox.Image.Height);
            g = Graphics.FromImage(buffer);
            g.DrawImage(form.ImageBox.Image, 0, 0);
            foreach (GRect gRest in form.result.Rects) {
                Draw(g, gRest.Left, gRest.Top, gRest.Right, gRest.Bottom);
            }
            if (dragging)
            {
                Draw(g, e.X, e.Y, startX, startY);
            }
            g = form.ImageBox.CreateGraphics();
            g.DrawImage(buffer, 0, 0);
        }
        public override void KeyPress(KeyPressEventArgs e)
        {
            if (e.KeyChar == 27)
            {
                form.result.Canceled = true;
                form.Close();
            }
        }
    }
}
